package com.zsc.edu.gateway.modules.iot.tsl.controller;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsc.edu.gateway.modules.iot.tsl.dto.ServeDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Serve;
import com.zsc.edu.gateway.modules.iot.tsl.query.ServeQuery;
import com.zsc.edu.gateway.modules.iot.tsl.service.ServeService;
import com.zsc.edu.gateway.modules.operationLog.entity.OperationLogAnnotation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * @author Yao
 */
@AllArgsConstructor
@RestController
@RequestMapping("api/rest/tsl/serve")
public class ServeController {

    private final ServeService serveService;


    /**
     * 创建服务
     *
     * @param dto 创建的服务
     * @return
     */
    @PostMapping
    @PreAuthorize("hasAnyAuthority('iot:serve:create')")
    @OperationLogAnnotation(content = "'服务'", operationType = "新建")
    public Serve create(@RequestBody ServeDto dto) {
        return serveService.create(dto);
    }


    /**
     * 更新服务
     *
     * @return 更新后的服务
     */
    @PatchMapping("{id}")
    @PreAuthorize("hasAnyAuthority('iot:serve:update')")
    @OperationLogAnnotation(content = "'服务'", operationType = "更新")
    public Serve update(@RequestBody ServeDto dto, @PathVariable("id") Long id) {
        return serveService.update(dto, id);
    }


    /**
     * 查询服务
     *
     * @param query    服务的查询表单
     * @param page 服务参数
     * @return Page<Device> 服务分页数据
     */
    @GetMapping
    @PreAuthorize("hasAnyAuthority('iot:serve:query')")
    public Page<Serve> page(Page<Serve> page, ServeQuery query) {
        return serveService.page(page, query.wrapper());
    }

    /**
     * 删除服务
     *
     * @param id 服务编号
     * @return 被删除的服务
     */
    @DeleteMapping("{id}")
    @PreAuthorize("hasAnyAuthority('iot:serve:delete')")
    @OperationLogAnnotation(content = "'服务'", operationType = "删除")
    public Boolean delete(@PathVariable("id") Long id) {
        return serveService.delete(id);
    }

    /**
     * 服务
     * 查询详情
     *
     * @param id ID
     * @return 任务
     */
    @GetMapping("{id}")
    @PreAuthorize("hasAnyAuthority('iot:serve:query')")
    public Serve detail(@PathVariable("id") Long id) {
        return serveService.detail(id);
    }

}
